﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍

namespace GIFPlayerDemo
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        string filepath;
        public MainPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// 在此页将要在 Frame 中显示时进行调用。
        /// </summary>
        /// <param name="e">描述如何访问此页的事件数据。Parameter
        /// 属性通常用于配置页。</param>
              protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            setVideoSource();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //filepath = "ms-appx:///Assets/testgif.gif";
            if (filepath != null)
            {
                //Video.Source = new System.Uri(filepath);
                Video.Play();
                Video.MediaFailed+=Video_MediaFailed;
            }

        }

        private void Video_MediaFailed(object sender, ExceptionRoutedEventArgs e)
        {
            infortextblock.Text = e.ErrorMessage;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var state = Video.CurrentState;
            string output = "高度";
            output += Video.NaturalVideoHeight;
            output += " 宽度";
            output += Video.NaturalVideoWidth;
            output += " 时间";
            output += Video.Position.ToString();
            infortextblock.Text = output;
        }

        private async void setVideoSource()
        {
            var picker = new Windows.Storage.Pickers.FileOpenPicker();
            picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
            picker.FileTypeFilter.Add(".gif");
            picker.FileTypeFilter.Add(".mpg");
            StorageFile file = await picker.PickSingleFileAsync();

            if (file != null)
            {
                filepath = file.Path;
                try
                {
                    var stream = await file.OpenAsync(FileAccessMode.Read);

                    Video.SetSource(stream, file.ContentType);
                }
                catch (Exception ex)
                {

                }
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            setVideoSource();
        }
    
    }
}
